﻿
/**
 * Module dependencies.
 */

var express = require('express')
	, routes = require('./routes')
	, user = require('./routes/user')
	, http = require('http')
	, path = require('path');

var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);

app.configure(function(){
	app.set('port', process.env.PORT || 3000);
	app.set('views', __dirname + '/views');
	app.set('view engine', 'jade');
	app.use(express.favicon());
	app.use(express.logger('dev'));
	app.use(express.bodyParser());
	app.use(express.methodOverride());
	app.use(express.cookieParser('your secret here'));
	app.use(express.session());
	app.use(app.router);
	app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
	app.use(express.errorHandler());
});


/*
//mindmap 이용해서 표기... 일단은 테이블에 리스트업!!!
app.get('/notelist',function(req,res){});


//노트 작성 폼
app.get('/note',function(req,res){});
//노트 읽기
app.get('/note/:noteid',function(req,res){});
//노트 신규 등록
app.post('/note/:noteid',function(req,res){});
//기존 노트 업뎃
app.put('/note/:noteid',function(req,res){});
//기존 노트 삭제...아마 이건 안쓸듯!!
app.delete('/note/:noteid',function(req,res){});
*/


var CheckLogin = function(req,res,next){
	if(req.session.user){
		console.log(req.session.user);
		next();
	} else {
		res.redirect('/login?redirect='+req.path);
	}
}

app.get('/', function(req,res){
	res.redirect('/login');
});

app.get('/login', function(req,res){
	if(req.session.user){
		res.redirect('/notelist');
	} else {
		if(req.query.redirect){
			res.render('login',{login:req.session.user,redirect:req.query.redirect});
		} else {
			res.render('login',{login:req.session.user,redirect:''});
		}
	}
});

app.post('/login', function(req, res){
	if(req.param('userpass') == 'asdf' ){
		req.session.user = req.param('userid');
		console.log(req.session.user);
		if( req.param('redir') != '' ){
			console.log("true : "+req.param('redir'));
			res.redirect(req.param('redir'));
		} else {
			console.log("false : "+req.param('redir'));
			res.redirect('/notelist');
		}
	} else {
		console.log("login fail");
		res.render('login',{login:req.session.user,redirect:req.param('redir')});
	}
});

app.get('/logout', function(req, res){
	req.session.destroy();
	res.redirect('/login');
});

app.get('/notelist', CheckLogin, function(req,res){
	console.log('/notelist come come');
	res.redirect('/note');
});

app.get('/note', CheckLogin, function(req,res){
	res.render('note',{login:req.session.user});
});

app.post('/chat', function(req, res) {
	console.log("pass : ",req.param('userpass'))
	console.log("pass : ",req.param('userid'))
	var tmpUri = '/chat/'+req.param('userid');
	if(req.param('userpass') == 'asdf') {
		res.redirect(tmpUri);
	} else {
		res.redirect('/login');
	}
});
app.get('/chat/:id', function(req, res){
	res.render('chat',{title:"hello"});
});


io.sockets.on('connection', function(socket){
	socket.on('join', function (data){
		console.log(data + "## room join ##");
		socket.join(data);
		socket.set('room', data);
	});
	socket.on('message', function (data){
		console.log(data);
		socket.get('room', function ( error, room) {
			io.sockets.in(room).emit('message', data);
		});
	});
});


server.listen(app.get('port'), function(){
	console.log("Express server listening on port " + app.get('port'));
});
